//
//  WJEssenceViewController.m
//  BeDeJie
//
//  Created by jerry on 2017/1/25.
//  Copyright © 2017年 jerry. All rights reserved.
//

#import "WJEssenceViewController.h"
#import "WJAllViewController.h"
#import "WJVideoViewController.h"
#import "WJVoiceViewController.h"
#import "WJPictureViewController.h"
#import "WJWordViewController.h"
#import "WJTitleButton.h"

@interface WJEssenceViewController () <UIScrollViewDelegate>

@property (nonatomic, weak) UIView *titleView;  //标题栏
@property (nonatomic, weak) WJTitleButton *previousClickedTitleButton;   //上一次点击的标题按钮
@property (nonatomic, weak) UIView *titleUnderlineView;   //标题下划线
@property (nonatomic, weak) UIScrollView *scrollView;  //用来存放所有子控制器

@end

@implementation WJEssenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置背景色
    self.view.backgroundColor = WJColor(206, 206, 206);
    
    //初使化子控制器
    [self setupAllChildController];
    
    //设置导航条
    [self setupNavBar];
    
    //设置UIScrollView
    [self setupScrollView];
    
    //设置标题栏
    [self setupTitleView];
    
}

//初使化子控制器
-(void) setupAllChildController {
    [self addChildViewController:[[WJAllViewController alloc] init]];
    [self addChildViewController:[[WJVideoViewController alloc] init]];
    [self addChildViewController:[[WJVoiceViewController alloc] init]];
    [self addChildViewController:[[WJPictureViewController alloc] init]];
    [self addChildViewController:[[WJWordViewController alloc] init]];
}

-(void)setupNavBar {
    //左边的按钮
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"icon_navbar_game_normal"]
                                                                 highImage:[UIImage imageNamed:@"icon_navbar_game_selected"]
                                                                    target:self
                                                                    action:@selector(game)];
    //右边的按钮
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"icon_navbar_share_normal"]
                                                                  highImage:[UIImage imageNamed:@"icon_navbar_share_selected"] target:self
                                                                     action:@selector(share)];
    //中间的标题
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navbar_main_title"]];
}

-(void)game{
    WJLog(@"game click");
}

-(void)share{
    WJLog(@"share click");
}

//设置UIScrollView
-(void) setupScrollView {
    //不允许自动修改UIScrollView的内边距
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //创建UIScrollView
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = [UIColor blueColor];
    scrollView.frame = self.view.bounds;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    scrollView.scrollsToTop = NO; // 点击状态栏的时候，这个scrollView不会滚动到最顶部
    [self.view addSubview:scrollView];
    
    self.scrollView = scrollView;
    
    NSInteger count = self.childViewControllers.count;
    CGFloat scrollViewWidth = scrollView.wj_width;
    //设置UIScrollView的滚动范围,水平方向可以滚动，垂直方向不能滚动
    scrollView.contentSize = CGSizeMake(count * scrollViewWidth, 0);
}

//设置标题栏
-(void) setupTitleView {
    UIView *titleView = [[UIView alloc] init];
    //设置有一定透明度的背景色
    titleView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
    //因为顶部有导航条，因此Y值＝导航条的高度+状态栏的高度
    titleView.frame = CGRectMake(0, WJNavMaxY, self.view.wj_width, WJTitleViewHeight);
    [self.view addSubview:titleView];
    
    self.titleView = titleView;
    
    //设置标题栏按钮
    [self setupTitleButton];
    
    //设置标题下划线
    [self setupTitleUnderline];
    
}

//设置标题栏按钮
-(void) setupTitleButton {
    NSArray *titles = @[@"全部",@"视频",@"声音",@"图片",@"段子"];
    NSUInteger count = titles.count;
    
    //标题按钮的大小
    CGFloat titleButtonWidth = self.titleView.wj_width / count;
    CGFloat titleButtonHeight = self.titleView.wj_height;
    
    //创建５个标题按钮
    for(NSUInteger i = 0; i< count; i++){
        WJTitleButton *titleButton = [[WJTitleButton alloc] init];
        //设置tag
        titleButton.tag = i;
        //设置frame
        titleButton.frame = CGRectMake(i * titleButtonWidth, 0, titleButtonWidth, titleButtonHeight);
        //设置文字
        [titleButton setTitle:titles[i] forState:UIControlStateNormal];
        //设置点击事件
        [titleButton addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.titleView addSubview:titleButton];
    }
}

//设置标题下划线
-(void) setupTitleUnderline {
    //获取第一个标题按钮。目的：需要使用标题按钮的颜色
    WJTitleButton *firstTitleButton = self.titleView.subviews[0];
    
    //下划线
    UIView *titleUnderline = [[UIView alloc] init];
    titleUnderline.wj_height = 2;
    titleUnderline.wj_y = self.titleView.wj_height - titleUnderline.wj_height;
    titleUnderline.backgroundColor = [firstTitleButton titleColorForState:UIControlStateSelected];
    [self.titleView addSubview:titleUnderline];
    
    self.titleUnderlineView = titleUnderline;
    
    // 让label根据文字内容计算尺寸
    [firstTitleButton.titleLabel sizeToFit];
    // 相当于点击了第一个titleButton
    [self titleButtonClick:firstTitleButton];
}

-(IBAction)titleButtonClick:(WJTitleButton *) titleButton {

    // 重复点击了标题按钮
    if (self.previousClickedTitleButton == titleButton) {
        //发出重复点击了标题按钮的通知
        [[NSNotificationCenter defaultCenter] postNotificationName:WJTitleButtonDidRepeatClickNotification object:nil];
    }
    
    //处理标题按钮点击
    [self dealTitleButtonClick:titleButton];
}

//处理标题按钮点击
-(void)dealTitleButtonClick:(WJTitleButton *) titleButton {
    //切换按钮状态
    self.previousClickedTitleButton.selected = NO;
    titleButton.selected = YES;
    self.previousClickedTitleButton = titleButton;
    
    NSInteger index = titleButton.tag;
    [UIView animateWithDuration:0.25 animations:^{
        //获取下划线长度的方法
        self.titleUnderlineView.wj_width = titleButton.titleLabel.wj_width + 10;
        //设置下划线将要移动到的X轴中心点
        self.titleUnderlineView.wj_centerX = titleButton.wj_centerX;
        
        //滚动scrollView
        CGFloat offsetX = self.scrollView.wj_width * titleButton.tag;
        //self.scrollView.contentOffset.y 表示偏移y值保持不变
        self.scrollView.contentOffset = CGPointMake(offsetX, self.scrollView.contentOffset.y);
    } completion:^(BOOL finished) {
        //此处使用懒加载的方式，加载子控制器的view
        [self addChildViewControllerIntoScrollView:index];
    }];
    
    // 设置index位置对应的tableView.scrollsToTop = YES， 其他都设置为NO
    for (NSUInteger i = 0; i < self.childViewControllers.count; i++) {
        UIViewController *childViewController = self.childViewControllers[i];
        // 如果view还没有被创建，就不用去处理
        if (!childViewController.isViewLoaded) continue;
        
        //获取子控制器对应的View
        UIScrollView *scrollView = (UIScrollView *)childViewController.view;
        if (![scrollView isKindOfClass:[UIScrollView class]])
            continue;
        
        // 是标题按钮对应的子控制器,就设为YES,其他设为NO
        scrollView.scrollsToTop = (i == index);
    }
}

-(void) addChildViewControllerIntoScrollView:(NSInteger)index {
    //根据index的位置，取出按钮对应的子控制器
    UIViewController *childViewController = self.childViewControllers[index];
    
    //如果view已经被加载过，就直接返回
    if (childViewController.isViewLoaded) {
        return;
    }
    
    //取出子控制器对应的view
    UIView *childView = childViewController.view;
    
    //设置子控制器view的frame
    CGFloat scrollViewWidth = self.scrollView.wj_width;
    CGFloat scrollViewHeight = self.scrollView.wj_height;
    childView.frame = CGRectMake(index * scrollViewWidth, 0, scrollViewWidth, scrollViewHeight);
    
    //添加子控制器的view到scrollView中
    [self.scrollView addSubview:childView];
}

#pragma mark UIScrollViewDelegate代理方法
//当用户松开scrollView并且滑动结束时调用此方法
-(void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //标题按钮的索引
    NSInteger index = scrollView.contentOffset.x / scrollView.wj_width;
    //点击对应的标题按钮
    WJTitleButton *titleButton = self.titleView.subviews[index];
    [self dealTitleButtonClick:titleButton];
}


@end
